{% extends "base/base.html" %}
{% block body %}
	<div align=center class='treeBtnDiv'>
		<button class='btn' callFunc='click:openShow'>预览</button>
		<button class='btn' callFunc='click:saveForm'>创建表单</button>
		<button class='btn' callFunc='click:htmlAndJs'>创建html</button>
	</div>
	<div class='blockContent treeBlock' id='treeBlock'>
		<div class='fieldTree'>
			<div class='title'>字段定义 <a callFunc='click:addField' class='addBtn'></a></div>
			<div class='body fieldBody'>
				
			</div>
		</div>
		<div class='fieldList'>
			<div class='title'>模板列表</div>
			<div class='body listBody' bindfunc='bindTemplate'>
			</div>
		</div>
		<div class='fieldParam'>
			<div class='title'>参数配置</div>
			<div class='body paramBody'>
				<div id='paramDiv'></div>
				<div class='centerCtrl'>
					<button class='btn' callFunc='click:saveParam'>保存参数</button>
					<button class='btn' callFunc='click:resetParam'>重置</button>
				</div>
			</div>
		</div>
	</div>
{% endblock %}
{% block script %}
	var editDiv=$("#paramDiv");
	var block=$("#treeBlock");
	var fieldBody=block.find(".fieldBody");
	var listBody=block.find(".listBody");
	var paramBody=block.find(".paramBody");
	var defField={name:'新增节点',id:"2",param:{name:'新增节点'},fieldType:"Input1.0"};
	//当前字段
	var curField=null;
	//当前模板
	var curTemp=null;
	var id=$.search("id");
	var baseField={name:'html头文件',id:"52",fieldType:"BaseHead1.0",param:{name:'html头文件',pageTitle:'web',devModel:true}};
	function setField(obj){
		fieldBody.find(".check").removeClass("check");
		obj.addClass("check");
		curField=obj;
	};
	function setTemp(obj){
		curTemp=obj;
	};
	yanhx.addFunc("checkHasSub",function(data){
		return data.param.hasSub;
	});
	yanhx.addFunc("openShow",function(){
		var temp=fieldBody.fillTree("getAttr","body");
		var bean=getForm(temp);
		$.postJSON("/do?action=htmlHandle.preShow",{json:JSON.stringify(bean)},function(data){
			window.open("/do?action=htmlHandle.openPre&id="+data,target="_blank");
		});
	});
	function getChild(elem){
		var ret=[];
		elem.children(".tree_canDel").each(function(i){
			var t=$(this);
			var d=t.data("field");
			ret.push(d);
			var ch=t.children(".tree_canDel");
			if(ch.length){
				d.childs=getChild(t);
			}
		});
		return ret;
	}
	function getForm(elem){
		var ret=[];
		var head=null;
		elem.children(".tree_canDel").each(function(i){
			var t=$(this);
			var d=t.data("field");
			//取第一个节点作为head节点
			if(i==0){
				head=d;
				return;
			}
			ret.push(d);
			var ch=t.children(".tree_canDel");
			if(ch.length){
				d.childs=getChild(t);
			}
		});
		return {
			head:head,
			rows:ret
		};
	};
	yanhx.addFunc("htmlAndJs",function(){
		$.postJSON("/do?action=htmlHandle.saveHtml",{id:id},function(data){
			$.warn(data.message);
		});
	});
	yanhx.addFunc("saveForm",function(){
		var temp=fieldBody.fillTree("getAttr","body");
		var bean=getForm(temp);
		$.postJSON("/do?action=htmlHandle.saveJson",{id:id,json:JSON.stringify(bean)},function(data){
			$.warn(data.message);
		});
	});
	yanhx.addFunc("fillItem",function(data){
		if(data){
			this.curRow.data("field",data);
		}
		if(data&&data.id==52){
			this.curRow.append('<a callfunc="click:nextField" class="addBtn"></a>');
			this.curRow.append('<a callfunc="click:modField" class="modBtn" title="编辑"></a>');
		}else{
			this.curRow.children("span.treeText").attr("drag","true").attr("callFunc","drop:sortField");
			this.curRow.append('<a callfunc="click:delField" class="delBtn"></a>');
			this.curRow.append('<a callfunc="click:nextField" class="addBtn"></a>');
			this.curRow.append('<a callfunc="click:modField" class="modBtn" title="编辑"></a>');
			this.curRow.append('<a callfunc="click:copyField" class="copyBtn" title="复制字段并添加到下一节点"></a>');
		}
		yanhx.exec("idHandle.get",this.curRow);
	});
	yanhx.addFunc("sortField",function(){
		var obj=yanhx.exec("dragHandle.getParam","dropOn");
		if(!yanhx.exec("idHandle.isEq",{src:this.parent(),obj:obj.parent()})){
			this.parent().insertAfter(obj.parent());
		}
	});
	yanhx.addCommon("load",function(){
		fieldBody.listTree({
			bindAll:true,
			schema:{
				subKey:'childs',
				subName:'name',
				subIdKey:'id',
				parentIdKey:'id',
				parentName:'name'
			},
			hasSub:yanhx.findFunc("checkHasSub"),
			fillItem:yanhx.findFunc("fillItem")
		});
		$.postJSON("/do?action=template.queryAll",null,function(data){
			listBody.list(data,true,{
				noTitle:true
			});
			$.getJSON("/do?action=htmlHandle.queryForm",{id:id},function(data){
				var temp=null;
				if(data.form){
					temp=JSON.parse(data.form);
					if(temp.head)
						baseField=temp.head;
				}
				fieldBody.fillTree("bindRow",[baseField]);
				listBody.find("#l_"+baseField.id).children().fire("click");
				paramBody.fill(baseField.param);
				var curRow=fieldBody.fillTree("getCurRow");
				curRow.find(".delBtn").remove();
				curRow.find(".copyBtn").remove();
				curRow.data("field",baseField);
				curRow.find(".treeText").removeAttr("drag").unbind();
				setField(curRow);
				if(temp)
					fieldBody.fillTree("bindTree",temp.rows);
			});
		});
	});
	yanhx.addFunc("checkTemplate",function(){
		//未变化 则不处理
		var li=this.parent();
		if(li.hasClass("check"))
			return;
		li.parent().children(".check").removeClass("check");
		li.addClass("check");
		var d=li.data("data");
		setTemp(li);
		if(d&&d.code&&d.code.constructor==String){
			d.code=JSON.parse(d.code);
		}
		yanhx.exec("_form_param",{
			data:d.code,
			elem:editDiv
		});
		if(curField&&curField.data("field")){
			editDiv.fill(curField.data("field").param);
		}
	});
	yanhx.addFunc("copyField",function(){
		var temp=this.parent().data("field");
		if(!temp){
			$.warn("未填写字段信息，无法复制");
			return;
		}
		var obj=fieldBody.fillTree("initRow",temp);
		obj.insertAfter(this.parent());
		listBody.find("#l_"+temp.id).children().fire("click");
		paramBody.fill(temp.param);
		yanhx.findFunc("fillItem").call({curRow:obj});
		setField(obj);
	});
	yanhx.addFunc("addField",function(){
		//新增节点 默认为2  即为输入框
		fieldBody.fillTree("bindRow",[defField]);
		listBody.find("#l_"+defField.id).children().fire("click");
		paramBody.fill(defField);
		var curRow=fieldBody.fillTree("getCurRow");
		setField(curRow);
	});
	//添加新节点到下一节点
	yanhx.addFunc("nextField",function(){
		var obj=fieldBody.fillTree("initRow",defField);
		obj.insertAfter(this.parent());
		listBody.find("#l_"+defField.id).children().fire("click");
		paramBody.fill(defField);
		yanhx.findFunc("fillItem").call({curRow:obj});
		setField(obj);
	});
	yanhx.addFunc("delField",function(){
		var pe=this.parent();
		if(pe.hasClass("check")){
			curField=null;
		}
		this.parent().remove();
	});
	yanhx.addFunc("modField",function(){
		var li=this.parent();
		if(li.hasClass("check")){
			return;
		}
		else{
			var d=li.data("field");
			if(!d)
				d=defField;
			listBody.find("#l_"+d.id).children().fire("click");
			paramBody.fill(d.param);
			setField(li);
		}
	});
	yanhx.addFunc("bindTemplate",function(data){
		return $("<div callFunc='click:checkTemplate'></div>").text(data.name+"("+data.jsonKey+data.version+")");
	});
	
	yanhx.addFunc("saveParam",function(){
		if(!curField){
			$.alert("未选择字段");
			return;
		}
		//表单节点为  id name fieldType subs param
		var bean=paramBody.getBean();
		yanhx.exec("descHandle.resetData",bean);
		var temp=curTemp.data("data");
		var data={id:temp.id,name:bean.name,fieldType:temp.jsonKey+temp.version,param:bean};
		//子节点变父节点
		if(bean.hasSub&&!curField.hasClass("hasSub")){
			changeNode({data:data,hasSub:true});
			curField.find(".copyBtn").remove();
			curField.find(".addBtn").attr("callFunc","click:addSubField");
			if(!curField.hasClass("open_sub")){
				curField.children(".tree_ctrl").trigger("click");
			}
		}
		//父节点变子节点  父->子会清空原子节点
		else if(!bean.hasSub&&curField.hasClass("hasSub")){
			changeNode({data:data,hasSub:false});
		}
		curField.data("field",data);
		curField.children("span.treeText").text(bean.name);
		$.warn("保存成功");
	});
	yanhx.addFunc("addSubField",function(){
		var curRow=fieldBody.fillTree("addSub",{
			data:defField,
			top:this.parent()
		});
		listBody.find("#l_"+defField.id).children().fire("click");
		paramBody.fill(defField);
		setField(curRow);
	});
	function changeNode(param){
		var newNode=fieldBody.fillTree("getRow",param);
		newNode.insertAfter(curField);
		curField.remove();
		curField=null;
		yanhx.findFunc("fillItem").call({curRow:newNode});
		curField=newNode;
	};
	yanhx.addFunc("resetParam",function(){
		curField.removeClass("check");
		curField.children(".modBtn").fire("click");
	});
{% endblock %}